﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace SumPower.MailAccount.Web
{
    public class ForceLoginAuthorizeAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            string key = "force_login";
            string[] values = new string[] { "1", "true" };
            var request = filterContext.HttpContext.Request;
            if (filterContext.HttpContext.User.Identity.IsAuthenticated && request.QueryString.AllKeys.Contains(key) && values.Contains(request.QueryString[key].ToLower()))
            {
                var url = new UrlHelper(filterContext.RequestContext);

                filterContext.Result = new RedirectResult(url.Action("SignOut", "Account", new { returnUrl = request.Url.ToString() }));
            }
        }
    }
}